package com.example.project.controller;

import com.example.project.dto.DepartmentDTO;
import com.example.project.entity.Department;
import com.example.project.service.DepartmentService;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * (Department)表控制层
 *
 * @author makejava
 * @since 2024-02-28 08:49:29
 */
@CrossOrigin
@RestController
@RequestMapping("department")
public class DepartmentController {
    /**
     * 服务对象
     */
    @Resource
    private DepartmentService departmentService;

    /**
     * 分页查询
     *
     * param department  筛选条件
     * param pageRequest 分页对象
     * @return 查询结果
     */
    @PostMapping("findPage")
    public ResponseEntity<Page<Department>> queryByPage(@RequestBody DepartmentDTO data) {
        com.example.project.entity.Page page = data.getPage();
        return ResponseEntity.ok(this.departmentService.queryByPage(data.getDepartment(), PageRequest.of(page.getPage(),page.getSize())));
    }
/**
 * 查询使用
 *
 * param department  筛选条件
 * param pageRequest 分页对象
 * @return 查询结果
 */
     @GetMapping("findAll")
     public ResponseEntity<List<Department>> query(){
         return ResponseEntity.ok(this.departmentService.queryAll());
     }
    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public ResponseEntity<Department> queryById(@PathVariable("id") Integer id) {
        return ResponseEntity.ok(this.departmentService.queryById(id));
    }

    /**
     * 新增数据
     *
     * @param department 实体
     * @return 新增结果
     */
    @PostMapping("add")
    public ResponseEntity<Department> add(@RequestBody Department department) {
        return ResponseEntity.ok(this.departmentService.insert(department));
    }

    /**
     * 编辑数据
     *
     * @param department 实体
     * @return 编辑结果
     */
    @PutMapping("put")
    public ResponseEntity<Department> edit(@RequestBody Department department) {
        return ResponseEntity.ok(this.departmentService.update(department));
    }

    /**
     * 删除数据
     *
     * param id 主键
     * @return 删除是否成功
     */
    @DeleteMapping("del")
    public ResponseEntity<Boolean> deleteById(@RequestBody Department data) {
        return ResponseEntity.ok(this.departmentService.deleteById(data.getId()));
    }

}

